﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ATSBLL;
using ATSModel;

public partial class ProductManage_ANTProductManage_TProductManage : System.Web.UI.Page
{
    private TProductType productType;

    public ProductManage_ANTProductManage_TProductManage()
    {
        productType = new TProductType();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindProductType();
        }
    }

 

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindProductType();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindProductType();
    }

    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if (e.Row.RowType == DataControlRowType.DataRow)
    //    {
    //        if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
    //        {
    //            ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
    //        }
    //    }
    //}


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string productTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        productType.Delete(productTypeID);
        BindProductType();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindProductType();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TProductTypeInfo productTypeInfo = new TProductTypeInfo();
        productTypeInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        productTypeInfo.ProductTypeName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        productTypeInfo.ProductTypeSerialID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();

        productType.Update(productTypeInfo);
        GridView1.EditIndex = -1;
        BindProductType();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        TProductTypeInfo productTypeInfo = new TProductTypeInfo();
        productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.ProductTypeName = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.ProductTypeSerialID = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
    
        CheckBox check = (CheckBox)(d.Rows[3].Cells[1].FindControl("ZTE"));
        if (check.Checked == true)
            productTypeInfo.ProductTypeIsZTE = 1;
        else
            productTypeInfo.ProductTypeIsZTE = 0;

        CheckBox checks = (CheckBox)(d.Rows[4].Cells[1].FindControl("IsRCode"));
        if (checks.Checked == true)
            productTypeInfo.IsReviewCode = 1;
        else
            productTypeInfo.IsReviewCode = 0;


        TProductTypeInfo pro = productType.LoadEntity(productTypeInfo.ProductTypeID);
        if (pro.ProductTypeID == productTypeInfo.ProductTypeID)
        {
            string str1 = "产品类型编号已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            productType.Insert(productTypeInfo);
            string str1 = "添加成功！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            BindProductType();
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindProductType();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    /// <summary>
    /// 根据产品类型检索产品类型配置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    string productTypeID = string.Empty;
    protected void butQuery_Click(object sender, EventArgs e)
    {
        productTypeID = this.tbProductTypeSn.Text.Trim();
        BindProductType();
    }

    /// <summary>
    /// 查询所有产品类型列表数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butAllPtList_Click(object sender, EventArgs e)
    {
        productTypeID = string.Empty;
        BindProductType();
    }


    /// <summary>
    /// 绑定页面数据
    /// </summary>
    protected void BindProductType()
    {
        if (string.IsNullOrEmpty(productTypeID))
            this.GridView1.DataSource = productType.LoadEntities();
        else
            GridView1.DataSource = productType.GetbyPID(productTypeID);
        GridView1.DataBind();
    } 
}
